package com.zhenzhang.myblog.controller;

import com.zhenzhang.myblog.annotations.Authentication;
import com.zhenzhang.myblog.dao.CommentDao;
import com.zhenzhang.myblog.entity.Comment;
import com.zhenzhang.myblog.entity.MultiPageInfo;
import com.zhenzhang.myblog.entity.Reply;
import com.zhenzhang.myblog.entity.role.UserRole;
import com.zhenzhang.myblog.service.CommentService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@RestController
@RequestMapping("/api/comment")
public class CommentController {
    @Resource
    private CommentDao commentDao;

    @Resource
    private CommentService commentService;

    @Resource
    private HttpSession session;

    @GetMapping
    public MultiPageInfo<Comment> getCommentList(Integer aid,
                                                 Integer uid,
                                                 @RequestParam(defaultValue = "1") Integer page,
                                                 @RequestParam(defaultValue = "10") Integer size) {
        return commentService.getCommentList(aid,uid,page,size);
    }

    @GetMapping("/reply")
    public MultiPageInfo<Reply> getReplyList(Integer cid,
                                             Integer uid,
                                             @RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "4") Integer size) {
        return commentService.getReplyList(cid,uid,page,size);
    }

    @PostMapping
    @Authentication(role = {UserRole.USER, UserRole.ADMIN})
    public void addComment(@RequestBody @Validated Comment comment) {
        commentService.addComment(comment);
    }

    @PostMapping("/reply")
    @Authentication(role = {UserRole.USER, UserRole.ADMIN})
    public void addReply(@RequestBody @Validated Reply reply) {
        commentService.addReply(reply);
    }

    @DeleteMapping
    @Authentication(role = {UserRole.USER, UserRole.ADMIN})
    public void deleteComment(Integer id, Integer uid) {
        commentService.deleteComment(id, uid, session);
    }

    @DeleteMapping("/batch")
    @Authentication(role = {UserRole.USER, UserRole.ADMIN})
    public void batchDeleteComment(@RequestBody ArrayList<Integer> list) {
        commentService.batchDeleteComment(list);
    }

    @DeleteMapping("/reply")
    @Authentication(role = {UserRole.USER, UserRole.ADMIN})
    public void deleteReply(Integer id, Integer uid) {
        commentService.deleteReply(id, uid, session);
    }

    @DeleteMapping("/batchReply")
    @Authentication(role = {UserRole.USER, UserRole.ADMIN})
    public void batchDeleteReply(@RequestBody ArrayList<Integer> list) {
        commentService.batchDeleteReply(list);
    }

    @GetMapping("up/{id}")
    public int thumbsUp(@PathVariable Integer id) {
        commentDao.thumbsUp(id);
        return commentDao.getLikes(id);
    }

    @GetMapping("reply/up/{id}")
    public int thumbsUpReply(@PathVariable Integer id) {
        commentDao.thumbsUpReply(id);
        return commentDao.getReplyLikes(id);
    }

    @GetMapping("max")
    public int getMaxId() {
        return commentDao.getMaxId();
    }

    @GetMapping("reply/max")
    public int getReplyMaxId() {
        return commentDao.getReplyMaxId();
    }
}
